<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-11-02 15:36:46
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Purchase\StorageService;
use App\Http\Services\Service;
use App\Repositories\MpcMoistureDetailRepository;
use App\Repositories\MpcMoistureRepository;

use Illuminate\Database\Eloquent\Collection;

class MoistureService extends Service
    {
    public $Detail;
    public function __construct(MpcMoistureRepository $Repository, MpcMoistureDetailRepository $Detail)
        {
        $this->repository = $Repository;
        $this->Detail     = $Detail;
        }

    public function list()
        {

        $data = $this->repository->condition(request())->getGrid();

        return $data;
        }

    public function info()
        {
        $data = $this->repository->condition(request())->getOne();
        return $data;
        }
    protected function saveFromStorage()
        {
        return $this->repository->autosave(request()->toArray());
        }
    protected function save()
        {
        $data                           = new Collection();
        $request                        = request();
        $data['id']                     = $request->input('id');
        $data['purchase_storage_id']    = $request->input('purchase_storage_id');
        $data['result_id']              = $request->input('result_id');
        $data['perhank']                = $request->input('perhank');
        $data['hank']                   = $request->input('hank');
        $data['gross']                  = $request->input('gross');
        $data['tare']                   = $request->input('tare');
        $data['standard_resurgence']    = $request->input('standard_resurgence');
        $data['measured_resurgence']    = $request->input('measured_resurgence');
        $data['indicated_weight']       = $request->input('indicated_weight');
        $data['suttle_weight']          = $request->input('suttle_weight');
        $data['total_indicated_weight'] = $request->input('total_indicated_weight');
        $data['net_weight']             = $request->input('net_weight');
        $data['packets_number']         = $request->input('packets_number');
        $data['prohank']                = $request->input('prohank');
        $data['odd']                    = $request->input('odd');
        $data['details']                = array_map(function ($v)
            {
            if (!is_numeric($v['gross']) || $v['gross'] === 0) {
                fail("检验明细数据不允许空行！");
                }
            return $v;
            }, $request->input('details') ?? []);
        $data['lot_id']                 = $request->input('lot_id');

        //检验更新采购入库单
        $model      = $this->repository->autosave($data);
        $purch_data = $model->purchase_storage;
        //没有填写单据日期更新单据日期
        $purch_data->main->billdate = $purch_data->main->billdate ?? $purch_data->in_date;
        $purch_data->main->save();

        $purch_data->fect_quantity           = $request->input('fect_quantity');
        $purch_data->fect_auxiliary_quantity = $request->input('fect_auxiliary_quantity');
        $purch_data->location_id             = $request->input('location_id');
        $purch_data->batch                   = $model->batch;
        if (($purch_data->location_id ?? 0) == 0) {
            fail("库位必选！");
            }
        //保存附件
        $attachments = $request->input('attachments');
        if ($attachments && is_array($attachments)) {
            $attachments_ids = array_map(
                function ($attachment)
                    {
                    return $attachment['id'];
                    }
                ,
                $attachments
            );
            $this->repository->saveAttachment($attachments_ids, $purch_data);
            }

        //保存派车单 
        $carry = ($request->input('carry'));
        if (isEmpty($carry["company"]["name"] ?? "")) {
            $purch_data->removeCarryItem();
            }
        else
            $this->repository->saveRelation("carry", $request->input('carry'), $purch_data);

        if ($purch_data->carry_id > 0)
            $purch_data->addCarryItem();

        $purch_data->save();
        //单据审核
        // $request->merge(["id" => $model->purchase_storage->id]);
        StorageService::run("audit", [[$purch_data->id]]);
        return $model;
        }

    }